﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using HotelierChannelManagement.ManagementChannelService.Proxies;
using HotelierChannelManagement.ServiceAgents;
using HotelierChannelManagement.Commanding;

namespace HotelierChannelManagement.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        private Hotel currentHotel;
        private ObservableCollection<Hotel> hotels;
        private string statusMessage;

        public MainPageViewModel() : this(new HotelsServiceAgent())
        {
        }

        public MainPageViewModel(IHotelsServiceAgent serviceAgent)
        {
            if (!IsDesignTime)
            {
                if (serviceAgent != null)
                {
                    ServiceAgent = serviceAgent;
                }
                GetHotels();
                WireCommands();
            }
        }

        private void WireCommands()
        {
            UpdateHotelCommand = new RelayCommand(UpdateHotel);
            DeleteHotelCommand = new RelayCommand(DeleteHotel);
        }

        #region Commands

        public RelayCommand UpdateHotelCommand 
        { 
            get; 
            private set; 
        }

        public RelayCommand DeleteHotelCommand 
        { 
            get; 
            private set; 
        }

        #endregion

        #region Properties

        private IHotelsServiceAgent ServiceAgent { get; set; }

        public ObservableCollection<Hotel> Hotels 
        {
            get 
            {
                return hotels;
            }
            set 
            {
                if (hotels != value)
                {
                    hotels = value;
                    OnPropertyChanged("Hotels");
                }
            }
        }

        public Hotel CurrentHotel 
        {
            get 
            {
                return currentHotel;
            }
            set 
            {
                if (currentHotel != null)
                {
                    currentHotel = value;
                    OnPropertyChanged("CurrentHotel");
                    StatusMessage = String.Empty;
                    UpdateHotelCommand.IsEnabled = true;
                    DeleteHotelCommand.IsEnabled = true;
                }
            }
        }

        public string StatusMessage 
        {
            get 
            {
                return statusMessage;
            }
            set
            {
                if (statusMessage != value)
                {
                    statusMessage = value;
                    OnPropertyChanged("StatusMessage");
                }
            }
        }

        #endregion

        private void GetHotels()
        {
            ServiceAgent.GetHotels((s, e) => Hotels = e.Result);
        }

        public void UpdateHotel()
        {
            SaveHotel(ObjectState.Modified);
        }

        public void DeleteHotel()
        {
            SaveHotel(ObjectState.Deleted);
            Hotels.Remove(CurrentHotel);
            CurrentHotel = null;
        }

        private void SaveHotel(ObjectState state)
        {
            CurrentHotel.ChangeTracker.State = state;
            ServiceAgent.SaveHotel(CurrentHotel, (s, e) =>
            {
                StatusMessage = (e.Result.Status) ? "Success!" : "Unable to complete operation";
            });
        }
    }
}
